package com.bsoft.gol.hbs.service.impl;

import com.bsoft.gol.hbs.bean.request.QueryCopywriterConfigRequest;
import ctd.account.UserRoleToken;
import ctd.controller.exception.ControllerException;
import ctd.dictionary.Dictionary;
import ctd.dictionary.DictionaryItem;
import ctd.dictionary.controller.DictionaryController;
import ctd.spring.boot.annotation.RpcAccess;
import ctd.spring.boot.annotation.SsdevService;
import ctd.util.BeanUtils;
import ctd.util.annotation.RpcService;
import com.bsoft.gol.hbs.dao.BaseCopywriterConfigDAO;
import hcn.base.BaseCopywriterConfig;
import hcn.base.Organization;
import hcn.util.RpcUserToken;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import pcn.bean.consultation.CopywriterQo;
import com.bsoft.gol.hbs.service.BaseCopywriterConfigService;
import service.rpc.OrganizationInfoIntf;
import com.bsoft.gol.hbs.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 文案配置rpc服务
 *
 * @Author yaosj
 */
@SsdevService("baseCopywriterConfig")
public class BaseCopywriterConfigServiceImpl implements BaseCopywriterConfigService {

    @Autowired
    private BaseCopywriterConfigDAO baseCopywriterConfigDAO;

    @Autowired
    private OrganizationInfoIntf organizationInfoIntf;

    /**
     * 获取文案配置列表
     *
     * @return
     */
    @Override
    public List<BaseCopywriterConfig> queryAllCopywriterConfig(QueryCopywriterConfigRequest request) throws ControllerException {
        if (StringUtils.isEmpty(request.getObjectId())) {
            throw new ControllerException(0, "参数不能为空:objectId");
        }
        List<BaseCopywriterConfig> list = baseCopywriterConfigDAO.queryCopywriterConfigByParams(request.getModuleId(), request.getObjectId());
        if (CommonUtils.isNotEmpty(list) && StringUtils.isEmpty(list.get(0).getObjectName())) {
            Organization organization = organizationInfoIntf.getOrgDetailByOrgId(request.getObjectId());
            if (CommonUtils.isNotEmpty(organization)) {
                for (BaseCopywriterConfig baseCopywriterConfig : list) {
                    baseCopywriterConfig.setObjectName(organization.getFullName());
                }
            }
        }
        return list;
    }

    /**
     * 保存文案配置
     *
     * @param baseCopywriterConfig
     */
    @Override
    public void saveCopywriterConfig(BaseCopywriterConfig baseCopywriterConfig) throws ControllerException {
        if (baseCopywriterConfig.getId() > 0) {
            if (StringUtils.isEmpty(baseCopywriterConfig.getVersionNo())) {
                throw new ControllerException("版本号不能为空！");
            }
            String versionNo = baseCopywriterConfig.getVersionNo();
            BaseCopywriterConfig copywriterConfig_exist = baseCopywriterConfigDAO.getCopywriterConfigById(baseCopywriterConfig.getId());
            String versionNoExist = copywriterConfig_exist.getVersionNo();
            if (Double.parseDouble(versionNo) <= Double.parseDouble(versionNoExist)) {
                throw new ControllerException("当前维护版本必须大于历史版本！");
            }
            BeanUtils.copy(baseCopywriterConfig, copywriterConfig_exist);
            baseCopywriterConfigDAO.update(copywriterConfig_exist);
        } else {
            baseCopywriterConfigDAO.save(baseCopywriterConfig);
        }
    }

    /**
     * 文案配置详情
     *
     * @param objectId
     * @return
     */
    @Override
    public BaseCopywriterConfig getCopywriterConfigByCopywriterCode(String copywriterCode, String objectId) {
        BaseCopywriterConfig baseCopywriterConfig = baseCopywriterConfigDAO.getCopywriterConfigByCode(copywriterCode, objectId);
        return baseCopywriterConfig;
    }

    /**
     * 文案配置详情
     */
    @Override
    public BaseCopywriterConfig getCopywriterConfigDetail(int id) {
        BaseCopywriterConfig baseCopywriterConfig = baseCopywriterConfigDAO.getCopywriterConfigById(id);
        return baseCopywriterConfig;
    }

    /**
     * 初始化文案配置
     */
    @Override
    @RpcService
    public void initCopywriterConfig(String orgId) {
        BaseCopywriterConfig baseCopywriterConfig = null;
        Dictionary moduleDic = null;
        Dictionary copywriterDic = null;
        try {
            moduleDic = DictionaryController.instance().get("hcn.base.dictionary.HCN_svrItemCode");
            copywriterDic = DictionaryController.instance().get("hcn.base.dictionary.copywriter");
            List<DictionaryItem> moduleItems = moduleDic.getSlice(null, 0, null);
            List<DictionaryItem> copywriterItems = copywriterDic.getSlice(null, 0, null);
            String moduleId = "";
            String moduleName = "";
            String copywriterCode = "";
            String copywriterTitle = "";

            for (DictionaryItem moduleItem : moduleItems) {
                moduleId = moduleItem.getKey();
                moduleName = moduleItem.getText();
                for (DictionaryItem copywriterItem : copywriterItems) {
                    copywriterTitle = copywriterItem.getText();
                    copywriterCode = copywriterItem.getKey();
                    if (copywriterCode.indexOf(moduleId) > -1) {
                        baseCopywriterConfig = new BaseCopywriterConfig();
                        BaseCopywriterConfig baseCopywriterConfig_exist = baseCopywriterConfigDAO.getCopywriterConfigByCode(copywriterCode, RpcUserToken.getTenantId());
                        if (baseCopywriterConfig_exist == null) {
                            baseCopywriterConfig.setCopywriterCode(copywriterCode);
                            baseCopywriterConfig.setCopywriterTitle(copywriterTitle);
                            baseCopywriterConfig.setLevel("2");
                            baseCopywriterConfig.setType("1");
                            baseCopywriterConfig.setObjectId(RpcUserToken.getTenantId());
                            baseCopywriterConfig.setObjectName("健康城市");
                            baseCopywriterConfig.setModuleId(moduleId);
                            baseCopywriterConfig.setModuleName(moduleName);
                            baseCopywriterConfig.setVersionNo("1.0");
                            baseCopywriterConfigDAO.save(baseCopywriterConfig);
                        }
                    }
                }
            }

            List<Organization> orgList = null;
            if (CommonUtils.isNotEmpty(orgId)) {
                Organization organization = organizationInfoIntf.getOrgDetailByOrgId(orgId);
                if (null != organization) {
                    orgList = new ArrayList<>();
                    orgList.add(organization);
                }
            } else {
                orgList = organizationInfoIntf.findAllOrgMain(0, 0);
            }

            if (orgList != null && orgList.size() > 0) {
                for (Organization organization : orgList) {
                    for (DictionaryItem moduleItem : moduleItems) {
                        moduleId = moduleItem.getKey();
                        moduleName = moduleItem.getText();
                        for (DictionaryItem copywriterItem : copywriterItems) {
                            copywriterTitle = copywriterItem.getText();
                            copywriterCode = copywriterItem.getKey();
                            if (copywriterCode.indexOf(moduleId) > -1) {
                                BaseCopywriterConfig baseCopywriterConfig_exist = baseCopywriterConfigDAO.getCopywriterConfigByCode(copywriterCode, organization.getOrgId());
                                if (baseCopywriterConfig_exist == null) {
                                    baseCopywriterConfig = new BaseCopywriterConfig();
                                    baseCopywriterConfig.setCopywriterCode(copywriterCode);
                                    baseCopywriterConfig.setCopywriterTitle(copywriterTitle);
                                    baseCopywriterConfig.setLevel("3");
                                    baseCopywriterConfig.setType("1");
                                    baseCopywriterConfig.setObjectId(organization.getOrgId());
                                    baseCopywriterConfig.setObjectName(organization.getFullName());
                                    baseCopywriterConfig.setModuleId(moduleId);
                                    baseCopywriterConfig.setModuleName(moduleName);
                                    baseCopywriterConfig.setVersionNo("1.0");
                                    BaseCopywriterConfig tenantConfig = baseCopywriterConfigDAO.getCopywriterConfigByCode(copywriterCode, RpcUserToken.getTenantId());
                                    if (Objects.nonNull(tenantConfig)) {
                                        baseCopywriterConfig.setContent(tenantConfig.getContent());
                                    }
                                    baseCopywriterConfigDAO.save(baseCopywriterConfig);
                                }
                            }
                        }
                    }
                }
            }

        } catch (ControllerException e) {
            e.printStackTrace();
        }
    }

    /**
     * 文案配置详情 (APP)
     */
    @Override
    @RpcAccess(auth = false)
    public BaseCopywriterConfig getCopywriterByCode(CopywriterQo qo) {
        String token = UserRoleToken.getCurrentTenantId();
        BaseCopywriterConfig baseCopywriterConfig = new BaseCopywriterConfig();
        if (StringUtils.isNotEmpty(qo.getOrgId())) {
            baseCopywriterConfig = baseCopywriterConfigDAO.getCopywriterConfigByCode(qo.getConfigurationId(), qo.getOrgId());
            if (baseCopywriterConfig == null || StringUtils.isEmpty(baseCopywriterConfig.getContent())) {
                baseCopywriterConfig = baseCopywriterConfigDAO.getCopywriterConfigByCode(qo.getConfigurationId(), qo.getTenantId());
            }
        } else {
            baseCopywriterConfig = baseCopywriterConfigDAO.getCopywriterConfigByCode(qo.getConfigurationId(), qo.getTenantId());
        }
        return baseCopywriterConfig;
    }


    @Override
    public List<BaseCopywriterConfig> queryCopywriterConfig(String objectId, String moduleId) {
        return baseCopywriterConfigDAO.queryCopywriterConfig(objectId, moduleId);
    }

}
